Skip to content

fix: refuse dsh native compaction with 422, not auth-class 403 (#2843) - #2844

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Mintapril:2026-10-12_dsh-compaction-refusal-422
Oct 12, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Mintapril:2026-10-12_dsh-compaction-refusal-422

Conversation

@Mintapril

@Mintapril Mintapril commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2843

What

dshCompactionRefusal() (src/server/dsh-compaction-guard.ts) now answers 422 instead of 403 on every lane (openai / anthropic / responses / google). Body shapes are unchanged (dsh_compaction_refused, retryable: false, same message). The two code comments and the dsh.allowDshCompaction / BILI_ALLOW_DSH_COMPACTION docs (en + zh, regenerated via tools/gen-config-docs.mjs) follow the new status.

Why

The refusal is generated locally by bili. It is a policy decision about one request, but 403 tells the client something else:

  • HTTP semantics. 401/403 are the authentication/authorization class. Clients that classify errors by status code treat them as a credential failure, and some respond by disabling the credential instead of just failing the one request.
  • Wire consistency. Anthropic's error table pairs 403 with permission_error and invalid_request_error with 400. The anthropic-lane refusal currently sends 403 + invalid_request_error, which is self-inconsistent.
  • Precedent. 403 = 请求被服务端基于权限拒绝 导致任务打断 #1686 moved tunnel resolution failures off 403 for the same reason ("403 tells clients never will succeed").

The guard's documented intent — a policy refusal that the caller must not retry as transient — only needs a non-retryable, non-auth client error.

Observed impact (details in #2843): dsh core maps 401 || 403 to AUTH, and its GUI renders every AUTH failure as API 密钥无效 / API key is invalid. With a third-party subscription-pool provider plugin that answers AUTH by cooling the account down for 24h, each refused compaction disabled the user's account, and every later turn failed until dsh was restarted. That plugin's classification is its own concern; this PR only stops bili from emitting an auth-class status for a non-auth refusal.

Why 422 and not another status:

status problem
401 / 403 auth class: read as a credential failure
408 / 409 / 429 / 5xx OpenAI/Anthropic SDK shouldRetry retries them, so the refused call would be replayed
413 size class, misleading
400 viable, but reads as "malformed request"; the request is well-formed and refused by policy
422 client error, non-retryable, no auth semantics ✓

Wire note: only the HTTP status of bili's own local refusal changes. No upstream request or response bytes are touched, and both compression modes are unaffected because the guard runs before any forwarding. This is still a wire-visible status change, so it belongs in the human-review tier.

Tests

  • New regression test in tests/dsh-compaction-guard.test.ts: on every lane, the refusal status must be neither auth-class (401/403) nor transient-class (408/409/429/5xx). It fails on master (openai: 403 is auth-class) and passes here.
  • Updated the existing status assertions in tests/dsh-compaction-guard.test.ts, tests/issue2028-dsh-compaction-toggle.test.ts, tests/issue2490-ledger-floor-doomed-probe.test.ts, and tests/issue2709-passthrough-compaction-guard.test.ts.

Pre-flight (Windows, node 24.16.0, on current master cd02d2d)

  • npm run typecheck ✓
  • targeted suites (dsh-compaction-guard, issue2028, issue2490, issue2658, issue2709-*): 46/46 ✓
  • npm test: 6339 pass / 10 fail / 33 skipped. All 10 failures come from the local Windows environment (symlink EPERM, a stderr-pipe child timeout, DNS/socket behavior, host-detection tests picking up the DSH_* env of the shell I ran from, child exit 3221226505). They fail the same way on unmodified master, and none of them touch the files in this PR. Linux CI is authoritative.
  • npm run build ✓
  • node tools/gen-config-docs.mjs generate reports no drift; check-coverage ✓; scripts/config-surface-guard.mjs ✓

Config surface

No config field is added or changed. The only change is the status code mentioned in the existing dsh.allowDshCompaction / BILI_ALLOW_DSH_COMPACTION descriptions (403 → 422).

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR Review — #2844 (fixes #2843): dsh compaction refusal 403 → 422

Reviewed the full diff against current master (cd02d2d, the PR's stated base — up to date, mergeable). Verdict up front: 可合(人工层) — correct direction, root-cause fix at the source, no regressions found, regression test proven to have teeth. Wire-visible status change → stays in the human-review tier (no auto-merge). 24/28 CI check-runs already green on head 0315c16 (all guard jobs, build, registry e2e, every real-client e2e lane incl. dsh); the 4 full-suite test matrix jobs were still running at review time — let them land.

① Direction gate (原 issue) — #2843's end-to-end trace holds up: 403 is the credential-failure class for every client bili serves (dsh core 401||403 → AUTH; dsh-plugin-subscriptions pool → 24h account-scoped cooldown). The guard's intent — "policy refusal, must not be treated as transient" — needs only non-auth AND non-retryable; 403 satisfies neither well because it additionally asserts "your credentials are bad". Fixing the status at the producer (the guard's own emission) rather than filtering downstream is root cause, not 堵. The status table checks out: 422 is outside the OpenAI/Anthropic SDK retry sets ({408, 409, 429, ≥500}), is client-class, carries no auth semantics. Precedent #1686 consistent.

② Regression review (既有行为逐项) — audited every behavior on the touched paths:

  1. Envelope detection (marker-decisive [dsh] 原生压缩拦截门的 SHAPE 信号(≤4 条消息)在 dsh-compaction-basic 0.2.0-rc.2 上恒假 → 压缩静默落地 → 1510 条 fold 锚点永久失配,连刷 166 条 no-anchor-match 且面板仍报 netSaved 355.9M #2193, gzip decode-then-sniff, shape-drift logging) — code untouched → 保持.
  2. Refusal body bytes (dsh_compaction_refused, retryable:false, message with the 在bili网页配置界面支持关闭和开启dsh的自动压缩功能 #2028 opt-in instructions) — byte-identical, pinned by tests → 保持.
  3. dsh压缩与bili压缩冲突 #2490 ledger (refused-replay byte high-water mark, refusal counts, conflict attribution) — status-independent metadata → 保持 (issue2490 suite passes).
  4. allowDshCompaction opt-in forward path (在bili网页配置界面支持关闭和开启dsh的自动压缩功能 #2028) — untouched → 保持 (issue2028 refuse→forward→clear cycle passes).
  5. [Bug] #1001 归因缺口:客户端原生压缩落地后只剩「未确认档」——bili 已记录的 17 次拒绝(dshCompactionRefusals)与 10 次 DIRECT 旁路未被用于归因 #2709 passthrough-lane guard — same function, same placement, only the status moves → 保持 (incl. gzip path and opt-out relay).
  6. Both compression modes — guard fires before any forwarding/pipeline work; no mode-specific code touched → 不受影响.
  7. Upstream bytes — local refusal only, zero upstream request/response bytes touched → 不受影响 (as claimed).

高危域点名:自动更新/自更新路径 — untouched; dsh 相关功能 — this PR IS that domain, read line-by-line ✓;版本钉死/lockfile — untouched;systemd/env 模板 — untouched;网络/代理路径 — untouched.
本版本引入回归:无。

③ Code review — core is a 2-line status change in both branches of dshCompactionRefusal() (anthropic-shaped + generic). Both call sites (src/server/handle.ts:261 passthrough lane, src/server/handle.ts:1581 pipeline lane) pass refusal.status/body straight to the response writer — no internal code branches on the old 403 (grep-verified; the only other 403s in src/ are admin-endpoint loopback denials and the resign arm, see finding A). Two observations, neither blocking:

  • A (adjacent, same bug class, out of scope): src/apig-resign.ts:144,146 — the resign arm also answers unresignable signed requests with a local 403 (bili_resign_unavailable). Same mechanism family ("local policy refusal emits an auth-class status"), but a different subsystem with disjoint triggers (only body-covering signatures from schemes bili can't re-sign; codex/grok subscription pools send unsigned traffic, so no field evidence of harm today). Left alone deliberately; flagging so it can be tracked as a checklist item if you want the class closed out comprehensively.
  • B (cosmetic residual, accepted): the anthropic lane keeps error.type: "invalid_request_error" under 422 (Anthropic pairs that type with 400). Harmless — dsh classifies by HTTP status — and body-shape stability was this PR's deliberate contract (bodies pinned by tests). Changing the type would be its own wire-shape decision; correctly not done here.

④ Test validity — the new regression test pins the class constraint (∉ {401,403} ∧ ∉ {408,409,429,≥500} ∧ stays 4xx) on all four lanes, which is better than pinning the literal value: it catches the bug family even if someone later swaps in another wrong status. Teeth verified by replaying it against master's guard file: fails with exactly openai: 403 is auth-class, passes on the PR head. Existing exact-value assertions updated consistently across the four suites; repo-wide grep confirms no other test still expects 403 on this path. No tautologies, no mock-only assertions.

⑤ Diff cleanliness — one linear commit, 10 files, +40/−23, every line on-purpose; CONFIGURATION.md/.zh-CN.md regenerated from the yaml seed (I re-ran the generator: zero drift; check-coverage and config-surface-guard both pass); no whitespace churn, no lockfile noise. Clean.

Local verification (targeted only, per test discipline — full suite is CI's job): typecheck ✓; the four touched suites 35/35 ✓; teeth check above ✓; docs-drift/coverage/surface-guard ✓. The author's 10 local Windows full-suite failures are env-attributed and claimed identical on master; the Linux CI matrix is the authority here.


结构化八问:

  1. 是什么问题:bili 本地拒绝 dsh 原生压缩调用时返回 HTTP 403,dsh 核心与 dsh-plugin-subscriptions 把 401/403 一律判为 AUTH 凭证失败,导致整个订阅账号(codex/grok 池内)被冷却 24 小时,之后每一轮都报「API 密钥无效」——尽管 token 本身有效。
  2. 什么原因导致的:守卫选用的拒绝状态码语义错误。守卫的真实需求是「非 AUTH 且非可重试」,而 403 恰好落在所有下游客户端的凭证失败类里,额外断言了「你的凭证无效」这一不成立的语义;缺陷在产生方(守卫自身发出的状态),不在客户端。
  3. 影响范围与严重程度:影响订阅类 provider(codex/grok 等池化账号)走 bili 车道的 dsh desktop profile;触发条件为每次上下文压力点 dsh 发起原生压缩。严重度:高但有限——账号停摆 24h、会话期间完全不可用(每轮 AUTH 失败),重启 dsh 即恢复,无数据损坏、token 未失效。
  4. 怎么修复的:在源头改产生方的输出——dshCompactionRefusal() 全线路改发 422(client-error、策略拒绝语义、SDK 重试集之外),响应体字节不变,注释与 en/zh 配置文档同步。
  5. 是否有回归问题:无。检测逻辑、拒绝体字节、dsh压缩与bili压缩冲突 #2490 ledger、在bili网页配置界面支持关闭和开启dsh的自动压缩功能 #2028 放行路径、[Bug] #1001 归因缺口:客户端原生压缩落地后只剩「未确认档」——bili 已记录的 17 次拒绝(dshCompactionRefusals)与 10 次 DIRECT 旁路未被用于归因 #2709 透传守卫、两种压缩模式、上游字节——逐项审计均保持(见上文清单);fail-fast/阈值/时序/输出格式均未漂移。
  6. 是否破坏缓存:不涉及——拒绝路径从不触碰内核状态、会话基线、重试或去重键。
  7. 是否改了设计方向:不涉及——同一守卫、同一位置、同一响应体,仅状态语义纠正。
  8. 是否引入了新的配置:不涉及——无新键/env/默认值;仅既有 dsh.allowDshCompaction / BILI_ALLOW_DSH_COMPACTION 描述文字中的状态号 403→422。

行为变更披露(旧值→新值):bili 本地产生的 dsh 压缩拒绝响应 HTTP 状态:403 → 422(全部线路 openai/anthropic/responses/google)。原因:#2843 的 AUTH 误分类链。wire-visible,故留人工审核层。

收工判定:代码与测试均可合;待最后 4 个全量 test 矩阵 CI job 转绿即可由人工合并。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 补充:head 0315c16fa 的全部 28 个 CI check-run 已转绿(含最后收尾的 4 个全量 test 矩阵 job:ubuntu/windows × node 22/24),0 失败。审查结论维持:可合(人工层)——请人工合并。

dshCompactionRefusal() answered 403 on every lane. 401/403 is the credential-failure class (HTTP semantics; Anthropic pairs 403 with permission_error, while the anthropic-lane refusal paired it with invalid_request_error), so clients that classify by status read a local policy refusal as a credential failure and may disable the credential. 422 keeps the refusal a non-retryable client error (not 408/409/429/5xx) without the auth semantics; body shapes are unchanged.

Fixes #2843
@Mintapril
Mintapril force-pushed the 2026-10-12_dsh-compaction-refusal-422 branch from 0315c16 to 46f6e93 Compare October 12, 2026 01:07
@ranxianglei
ranxianglei merged commit 6fc3f9b into ranxianglei:master Oct 12, 2026
28 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.299-master.6fc3f9b (rolling master tag).

npm install -g billion-context@0.0.299-master.6fc3f9b  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] dsh compaction refusal returns 403 → subscription pools (codex/grok) park the account 24h as AUTH → “API 密钥无效”

2 participants